﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Acms.AcmsControls
{
    public class FloatingImageContentControlProperties : BaseContentControlProperties
    {
        public FloatingImageContentControlProperties()
        {
            Float = FloatOptions.Right;
        }

        public int ResourceId { get; set; }

        public bool HasResource()
        {
            return ResourceId > 0;
        }

        public string AltText { get; set; }

        public int Width { get; set; }

        public int Height { get; set; }

        public enum FloatOptions
        {
            Left,
            Right,
            Centre
        }

        public FloatOptions Float { get; set; }


        protected override void ReadValue(System.Xml.XmlReader reader)
        {
            if (reader.Name.ToLower() == "resourceid" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    ResourceId = reader.ReadContentAsInt();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "width" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    Width = reader.ReadContentAsInt();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "height" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    Height = reader.ReadContentAsInt();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "alttext" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    AltText = reader.ReadContentAsString();
                }
                catch
                {
                }
            }
            else if (reader.Name.ToLower() == "float" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    Float = (FloatOptions)Enum.Parse(typeof(FloatOptions), reader.ReadContentAsString());
                }
                catch
                {
                }
            }
        }

        protected override void  WriterPropertiesToXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("content");

            writer.WriteAttributeString("resourceId", ResourceId.ToString());
            writer.WriteAttributeString("width", Width.ToString());
            writer.WriteAttributeString("height", Height.ToString());
            writer.WriteAttributeString("float", Float.ToString());

            writer.WriteStartElement("alttext");
            writer.WriteAttributeString("search", "true");
            writer.WriteValue(this.AltText);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
    }
}
